﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCQuaTrinhCT : System.Web.UI.UserControl
    {

        NSQuaTrinhCongTac qt = new NSQuaTrinhCongTac();
        NSToChuc toChuc = new NSToChuc();
        private List<NSQuaTrinhCongTac> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            dtNgayHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            dtNgayHetHieuLuc.Text = string.Empty;
            txtNoiDung.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtCongViec.Text = string.Empty;
            txtCoQuanSuDung_QTCT.Text = string.Empty;
            hdToChucId_QTCT.Value = string.Empty;
            hdFlagToChuc_QTCT.Value = string.Empty;
            UCQuyetDinh.ResetControl(common.QuaTrinhCongTac);
        }
        [DirectMethod]
        public void ShowDialog(string type, string iD, string nhanvienID, string typeParent, string coQuanTuyenDung)
        {

            hdNhanVienId.Text = nhanvienID;
            hdId.Text = iD;
            if (type.Equals("AddNew"))
            {
                wdQTCT.Title = "Tạo mới quá trình công tác";
                ResetControl();
                hdType.Text = type;
                wdQTCT.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdQTCT.Title = "Sửa đổi quá trình công tác";
                hdType.Text = type;
                btnSaveQTCT.Hidden = false;
                LoadDataNBL(Convert.ToInt16(iD));
            }
            else if (type.Equals("View"))
            {
                wdQTCT.Title = "Xem chi quá trình công tác";
                hdType.Text = type;
                btnSaveQTCT.Hidden = true;
                LoadDataNBL(Convert.ToInt16(iD));
            }
            wdQTCT.Show();
        }
        public void LoadDataNBL(int id)
        {
            lst = (List<NSQuaTrinhCongTac>)Session["QTCT"];
            NSQuaTrinhCongTac item = lst.Where(a => a.QuaTrinhCongTacId.Equals(id)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayBatDau.Value.ToString("dd/MM/yyyy");
            dtNgayHetHieuLuc.Text = item.NgayKetThuc.HasValue && !item.NgayKetThuc.Value.Year.Equals(0001) ? item.NgayKetThuc.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtNoiDung.Text = item.NoiDung;
            txtCongViec.Text = item.CongViec;
            txtChucVu.Text = item.ChucVu;

            hdFlagToChuc_QTCT.Value = item.ToChucFlag.HasValue ? item.ToChucFlag.Value.ToString() : string.Empty;
            if (item.ToChucFlag.HasValue && item.ToChucFlag.Value)
            {
                txtCoQuanSuDung_QTCT.Text = item.CoQuanSuDungID > 0 ? toChuc.LayTenToChuc(item.CoQuanSuDungID.Value) : string.Empty;
                hdToChucId_QTCT.Value = item.CoQuanSuDungID.Value.ToString();
            }
            else
            {
                txtCoQuanSuDung_QTCT.Text = item.CoQuanKhac;
            }
            int qdID = item.QuyetDinhID.HasValue ? item.QuyetDinhID.Value : 0;
            hdQDID.Value = qdID.ToString();
            if (qdID > 0)
            {
                UCQuyetDinh.Command(common.CommandLoadByID, qdID);
            }

        }
        protected void btnSaveQTCT_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayHieuLuc = Convert.ToDateTime(dtNgayHieuLuc.Text);
            DateTime ngayHetHieuLuc = !dtNgayHetHieuLuc.Text.Equals("01/01/0001") ? Convert.ToDateTime(dtNgayHetHieuLuc.Text) : DateTime.MinValue;
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;

            int toChucId = 0;
            bool tochucFlag = Convert.ToBoolean(hdFlagToChuc_QTCT.Value);
            if (Convert.ToBoolean(hdFlagToChuc_QTCT.Value))
            {
                toChucId = !string.IsNullOrEmpty(hdToChucId_QTCT.Value.ToString()) ? Convert.ToInt32(hdToChucId_QTCT.Value) : 0;

            }

            if (hdType.Text.Equals("AddNew"))
            {
                //Tạo mới 1 quyết định
                int qdID = UCQuyetDinh.TaoMoiQuyetDinh();

                NSQuaTrinhCongTac item = qt.GetQTCT(nhanVienId, ngayHieuLuc, ngayHetHieuLuc, txtNoiDung.Text, toChucId, tochucFlag, txtCoQuanSuDung_QTCT.Text, txtChucVu.Text, txtCongViec.Text, qdID);
                ((UCNhanVien)this.Parent).GetQTCT(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                int qdID = 0;
                //Edit quyết định
                if (Convert.ToInt16(hdQDID.Value) > 0)
                {
                    UCQuyetDinh.Command(common.CommandSua, Convert.ToInt16(hdQDID.Value));
                }
                else
                {
                    //Nếu không có record nào thì sẽ tạo mới
                    qdID = UCQuyetDinh.TaoMoiQuyetDinh();
                }

                qt.EditQTCT(Convert.ToInt16(hdId.Text), ngayHieuLuc, ngayHetHieuLuc, txtNoiDung.Text, toChucId, tochucFlag, txtCoQuanSuDung_QTCT.Text, txtChucVu.Text, txtCongViec.Text, qdID);
                ((UCNhanVien)this.Parent).ReloadGrid("QTCT");
            }
            X.Mask.Hide();
            wdQTCT.Hide();

        }
        [DirectMethod]
        public void LoadLookUpToChuc_QTCT()
        {
            bool flag = !string.IsNullOrEmpty(hdFlagToChuc_QTCT.Value.ToString()) ? Convert.ToBoolean(hdFlagToChuc_QTCT.Value.ToString()) : true;
            UcToChuc.ShowLookUp_ToChuc("ToChucKhac", flag, txtCoQuanSuDung_QTCT.Text);
            X.Mask.Hide();
        }
    }
}